<?php

/*============================================
Classe IacmdPrecondMsg
============================================*/

class IacmdPrecondMsg extends ModelObject{

	protected static $cached_object_list= array();
	protected $COMNAME;
	protected $PRE_COND_MESSAGE;

	protected function __construct( $COMNAME,$PRE_COND_MESSAGE){
		try{
			
			$this->COMNAME = $COMNAME;
			$this->PRE_COND_MESSAGE = $PRE_COND_MESSAGE;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getCOMNAME(){
		return $this->COMNAME;
	}

	public function getPRECONDMESSAGE(){
		return $this->PRE_COND_MESSAGE;
	}

	public function setPRECONDMESSAGE( $PRE_COND_MESSAGE){
		try{
			$temp = $this->PRE_COND_MESSAGE;
			if($this->PRE_COND_MESSAGE !== $PRE_COND_MESSAGE){
				$this->PRE_COND_MESSAGE = $PRE_COND_MESSAGE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->PRE_COND_MESSAGE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setPRECONDMESSAGE() a échouée",0,$e);
		}
	}

	public static function create( $COMNAME,$PRE_COND_MESSAGE){
		try{
			$iacmdPrecondMsg = new IacmdPrecondMsg($COMNAME, $PRE_COND_MESSAGE);
			$iacmdPrecondMsg->insert();
			IacmdPrecondMsg::cacheObject($iacmdPrecondMsg);
			return $iacmdPrecondMsg;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type IacmdPrecondMsg a échouée",0,$e);
		}
	}

	public static function get( $COMNAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($COMNAME) || $COMNAME === "") {
				return null;
			}
			if(IacmdPrecondMsg::existsInCache($COMNAME)) {
				return IacmdPrecondMsg::getFromCache($COMNAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iacmd_precond_msg` WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$COMNAME));
			if ($row = $stmt->fetch()){
				$iacmdPrecondMsg = new IacmdPrecondMsg( $row['COMNAME'],$row['PRE_COND_MESSAGE']);
				$iacmdPrecondMsg->id =  $row['COMNAME'];
				$dbh->commit();
				IacmdPrecondMsg::cacheObject($iacmdPrecondMsg);
				return $iacmdPrecondMsg;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $COMNAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iacmd_precond_msg` SET `PRE_COND_MESSAGE` = :PRE_COND_MESSAGE WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':PRE_COND_MESSAGE'=>$this->PRE_COND_MESSAGE, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iacmd_precond_msg` WHERE `COMNAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			IacmdPrecondMsg::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iacmd_precond_msg` ( `PRE_COND_MESSAGE`) VALUES (:PRE_COND_MESSAGE)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':PRE_COND_MESSAGE'=>$this->PRE_COND_MESSAGE));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iacmd_precond_msg`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(IacmdPrecondMsg::existsInCache($row['COMNAME'])){
					$obj = IacmdPrecondMsg::getFromCache($row['COMNAME']);
				}
				else{
					$obj = new IacmdPrecondMsg( $row['COMNAME'],$row['PRE_COND_MESSAGE']);
				}
				if(!is_null($obj)){
					$obj->id = $row['COMNAME'];
					IacmdPrecondMsg::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type IacmdPrecondMsg a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iacmd_precond_msg`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type IacmdPrecondMsg a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iacmd_precond_msg`';
			$sql_select = 'SELECT COMNAME FROM `iacmd_precond_msg`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = IacmdPrecondMsg::getObjectFromCache($row['COMNAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						IacmdPrecondMsg::removeObjectFromCache($row['COMNAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type IacmdPrecondMsg a échouée",0,$e);
		}
	}

}